package ruleengine;

import static com.ruleengine.coordinator.SendWorkerClient.MESSAGE_LABELS_KEY_RULE_CHAIN_ID;

import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;

import com.ruleengine.app.RuleEngineApp;
import com.ruleengine.coordinator.SendWorkerClient;
import com.ruleengine.util.model.Message;
import com.ruleengine.util.model.Metadata;

import jakarta.annotation.PostConstruct;

/**
 * @author lianghanwei on 2024/3/23
 */
@SpringBootTest(classes = RuleEngineApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseTest {

    protected static final String DEMO_TENANT_ID = "tenant_id_1";
    protected static final String DEMO_INSTANCE_ID = "instance_id_1";
    protected static final String DEMO_RULEENGINE_ID = "ruleengine_id_1";
    protected static final String DEMO_RULECHAIN_ID = "rulechain_id_1";
    protected static final String DEMO_MESSAGE_CONTENT = "{ \"a\":1, \"b\":2, \"c\":[1,2,3,4,5] }";
    protected static final String DEMO_FILTER_EXPRESSION = "b!=2";
    protected static final String DEMO_MAP_EXPRESSION = "c";

    @LocalServerPort
    protected int port;

    @Autowired
    protected TestRestTemplate restTemplate;

    @Autowired
    protected SendWorkerClient sendWorkerClient;

    protected String path;

    @PostConstruct
    public void init() {
        path = "http://localhost:" + port + "/v1/ruleengine";
    }

    protected Message buildDemoMessage() {
        return buildDemoMessage(DEMO_RULECHAIN_ID);
    }

    protected Message buildDemoMessage(String rulechainId) {
        return Message.builder()
            .metadata(Metadata.builder()
                .labels(Map.of(MESSAGE_LABELS_KEY_RULE_CHAIN_ID, rulechainId))
                .build())
            .message(DEMO_MESSAGE_CONTENT.getBytes(StandardCharsets.UTF_8))
            .qos(0)
            .build();
    }

}
